package com.dns.customers.controller;

import com.dns.customers.AppPage;
import com.dns.customers.service.CustomerDnsService;
import com.dns.customers.service.DomainService;
import com.dns.customers.service.ExportService;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import com.util.XlsMain;
import net.sf.json.JSONObject;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * TODO  后期可能会用
 * @author :  yulei
 * @data :  2018/7/26 19:54
 * @Version :  1.0
 **/
@Controller
@RequestMapping("/export")
public class ExportController extends AppPage {
    //管理服务
    @Autowired
    CustomerDnsService customerDnsService;

    @Autowired
    ExportService exportService;

    public void exportOrderDetailPdf(HttpServletRequest request,HttpServletResponse response){
        OutputStream out =null;
        try {
            out=response.getOutputStream();
            Document document = new Document(PageSize.A4.rotate());
            PdfWriter.getInstance(document, out);
            exportService.exportOrderDetailPdf(document,request);
            document.close();
            out.flush();
            out.close();

        } catch (Exception e) {
            log.error("导出DNS信息异常:",e);
        }
    }

    /**
     * 导出EXCEL
     * @param request
     * @param response
     */
    @RequestMapping(value = "/exportOrderDetailByExcel", method=RequestMethod.GET)
    @ResponseBody
    public void exportOrderDetailByExcel(HttpServletRequest request,HttpServletResponse response){
        response.setContentType("octets/stream");
        OutputStream out=null;
        try {
             out= response.getOutputStream();
             String fileName="DNS订单详情明细";
             response.setCharacterEncoding("utf-8");
             String codedFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
             response.setHeader("Content-Disposition", "attachment; filename=" + codedFileName + ".xls");
             exportService.exportOrderDetailByExcel(request,out);

        } catch (Exception e) {
             log.error("导出订单信息异常:",e);
        }
         finally {
            //关闭输出流
            try {
                if(null!=out){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 批量导入根域名信息（回显）
     * @param file excel文件
     * @return
     */
    @RequestMapping("/importRootDomains")
    @ResponseBody
    public String importRootDomains(MultipartFile file){
        String  result = exportService.importRootDomains(file);
        return  result;
    }

    /**
     * 下载批量导入根域名信息模板（回显）
     * @param
     * @return
     */
    @RequestMapping("/downloadDomainTemplate")
    @ResponseBody
    public String downloadDomainTemplate (HttpServletRequest req, HttpServletResponse res,String fileName){

        JSONObject resultJson = new JSONObject();
        JSONObject message = new JSONObject();
        String downLoadPath = req.getScheme() + "://"
                + req.getServerName() + ":" + req.getServerPort()+req.getContextPath()+"/downloadTemplate/"+fileName;
        try{
            res.sendRedirect(downLoadPath);
//            XlsMain.download(req,res,fileName,downLoadPath);
        }catch (Exception e){
            message.put("reusultcode", "8888");
            message.put("message", e.getMessage());
            resultJson.put("result", message);
            e.printStackTrace();
            e.printStackTrace();
        }
        return  resultJson.toString();
    }
    /**
     * 批量导入子域名信息（回显）
     * @param file excel文件
     * @return
     */
    @RequestMapping(value = "/importChildDomains",produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String importChildDomains(MultipartFile file,String custId){
        String  result = exportService.importChildDomains(file,custId);
        return  result;
    }
    /**
     * 验证下发任务时的子域名是否正确
     */
    @RequestMapping(value="/checkDomainFormat",produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String checkDomainFormat(String custId,String domainName){
        return  exportService.checkDomainFormat(custId,domainName);
    }
}




